package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class  AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    private static final String ZONE = "0";
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrDao attrDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> qw = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            qw.lambda().and(s -> {
                s.eq(AttrGroupEntity::getAttrGroupId, key).or().like(AttrGroupEntity::getDescript, key);
            });
        }
        if (ZONE.equals(catelogId.toString())) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), qw);
            return new PageUtils(page);
        } else {
            qw.lambda().eq(AttrGroupEntity::getCatelogId, catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), qw);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        List<Long> collect = relationEntities.stream().map(s -> s.getAttrId()).collect(Collectors.toList());
        if (collect == null || collect.size() == 0) {
            return null;
        }
        List<AttrEntity> list = attrDao.selectBatchIds(collect);

        return list;
    }

    @Override
    public void removeattrRelationDelete(AttrVo[] attrVo) {
        List<AttrVo> collect = Arrays.stream(attrVo).collect(Collectors.toList());
        this.baseMapper.removeattrRelationDelete(collect);

    }

    @Override
    public List<AttrGroupWithAttrVo> getAttrgroupAndAttr(Long attrgroupId) {
        LambdaQueryWrapper<AttrGroupEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(AttrGroupEntity::getCatelogId, attrgroupId);
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(qw);
        List<AttrGroupWithAttrVo> collect=null;
        if (attrGroupEntities != null){
                collect = attrGroupEntities.stream().map(s -> {
                AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
                BeanUtils.copyProperties(s, attrGroupWithAttrVo);
                List<AttrEntity> attrRelation = this.getAttrRelation(s.getAttrGroupId());
                attrGroupWithAttrVo.setAttrs(attrRelation);
                return attrGroupWithAttrVo;

            }).collect(Collectors.toList());

        }
            return collect;
    }


}


//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<AttrGroupEntity> page = this.page(
//                new Query<AttrGroupEntity>().getPage(params),
//                new QueryWrapper<AttrGroupEntity>()
//        );
//
//        return new PageUtils(page);
//    }


